import Vue from 'vue'
import Main from './toast.vue'
let ToastConstructor = Vue.extend(Main)
let instance
const Toast = function (message,duration = 3000) {
  message = message || ''
  duration = duration || 3000
  instance = new ToastConstructor({
    data:{state:true,showContent:true,message}
  })
  instance.vm = instance.$mount()
  let isNode = document.querySelector('.toast')
  if (!isNode) {
    document.body.appendChild(instance.vm.$el)
  } else {
    document.body.removeChild(isNode)
  }
  setTimeout(() => {instance.showContent = false} ,duration - 1250)  // 过了 duration 时间后隐藏整个组件
  setTimeout(() => {instance.state = false} ,duration)
}
Vue.prototype.$toast = Toast
export default Toast
